<template>
	<view class="page_userSet">
		<view class="list">
			<view class="item">
				<text>头像</text>
				<view class="sub">
					<image class="user_img" src="https://yun.xssbdc.com/group1/M00/00/02/rBiDGl9jB1WAS_ptAAAnsAa132k614.png"></image>
					<uni-icons type="arrowright" color="#bcbcbc" size="24"></uni-icons>
				</view>
			</view>
			<view class="item" @click="changeName">
				<text>昵称</text>
				<view class="sub">
					{{userInfo.name}} 
					<uni-icons type="arrowright" color="#bcbcbc" size="24"></uni-icons>
				</view>
			</view>
		</view>
		<uni-popup ref="showtip" type="center" :mask-click="false">
			<view class="uni-tip">
				<text class="uni-tip-title">修改昵称</text>
				<view class="uni-tip-content">
					<input type="text" :placeholder="userInfo.name" v-model="userInfo.name" />
				</view>
				<view class="uni-tip-group-button">
					<text class="uni-tip-button border-r" @click="cancel('tip')">取消</text>
					<text class="uni-tip-button" style="color: #007aff;" @click="addNewName">确定</text>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { mapState} from "vuex";
	export default {
		data() {
			return {
				
			}
		},
		computed: {
			...mapState(["userInfo"]),
		},
		methods: {
			//修改名字
			changeName() {
				this.$refs.showtip.open()
			},
			//取消
			cancel(type) {
				this.$refs.showtip.close()
			},
			//添加新名字
			addNewName() {
				let that = this;
				let perfectAgent = {};
				perfectAgent.name = this.userInfo.name;
				perfectAgent.id = this.userInfo.id;
				this.$api.changeName(perfectAgent).then(res=> {
					if(res.data.code == 200){
						let userObj = JSON.parse(JSON.stringify(that.userInfo))
						userObj.name =  that.userInfo.name;
						userObj.haveName = true;
						that.$store.commit("SET_USERINFO", userObj );
						that.cancel("tip")
					}
				}).catch( err => {
					console.log(err)
					that.cancel("tip")
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	page{
		background: #F5F5F5;
	}
	/* #ifdef APP-PLUS*/
	.page_userSet{
		height: 100vh;
		background: #F5F5F5 ;
	}
	/* #endif */
	.list{
		padding-top: 40rpx;
	}
	.user_img{
		width: 80rpx;
		height: 80rpx;
		overflow: hidden;
		border-radius: 50%;
		margin-right: 16rpx;
	}
	.item {
		width: 100%;
		padding: 30rpx;
		height: 108rpx;
		border-bottom: 2px solid #F5F5F5;
		background: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 16px;
		box-sizing: border-box;
		line-height: 108rpx;
		.sub{
			display: flex;
			align-items: center;
			justify-content: space-between;
		}
	}
</style>
